<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/8/21
 * Time: 16:04
 */

namespace app\back\controller;
use app\common\model\Menu as MenuModel;
use think\Request;
use think\Session;

class Menu extends BaseController
{
    public function index()
    {
        $menus = MenuModel::getTreeData('tree', 'sort_number');
        $this->assign(compact('menus'));
        return view();
    }

    public function add()
    {
        $menus = MenuModel::getTreeData('tree', 'sort_number');
        $this->assign(compact('menus'));
        return view();
    }

    public function addProcess(Request $request)
    {
        if ($request->isPost()) {
            $model = new MenuModel();
            $model->pid         = $request->post('pid');
            $model->name        = $request->post('name');
            $model->sort_number = $request->post('sort_number');
            $model->url         = $request->post('url');
            $model->user_id     = Session::get('login.id');
            $model->save();
            if ($model->id) {
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $model->id]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10000']);
        }
        return json(['status' => 'failed', 'code'  =>  '99999']);
    }

    public function edit($id)
    {
        $model = new MenuModel();
        $data = $model->find($id)->toArray();
        $menus = $model->getTreeData('tree', 'sort_number');
        return view('edit', compact('menus', 'data'));
    }

    public function editProcess(Request $request)
    {
        if ($request->post('id')) {
            $model = MenuModel::find($request->post('id'));
            $model->pid         = $request->post('pid');
            $model->name        = $request->post('name');
            $model->sort_number = $request->post('sort_number');
            $model->url         = $request->post('url');
            $model->user_id     = Session::get('login.id');
            $model->save();
            if ($model->id) {
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $model->id]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10000']);
        }
        return json(['status' => 'failed', 'code'  =>  '99999']);
    }

    public function changeShow(Request $request)
    {
        if ($request->post('id')) {
            $model = MenuModel::find($request->post('id'));
            $model->is_show = $request->post('is_show');
            $model->user_id = 1;
            $model->save();
            if ($model->id) {
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $model->id]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10000']);
        }
        return json(['status' => 'failed', 'code'  =>  '99999']);
    }

    public function delete(Request $request)
    {
        if ($request->post('id')) {
            $model = MenuModel::get(['pid'=>$request->post('id')]);
            if (!$model) {
                MenuModel::destroy($request->post('id'));
                return json(['status' => 'success', 'code'  =>  '00000', 'result' => ['id' => $request->post('id')]]);
            }
            return json(['status' => 'failed', 'code'  =>  '10001']);
        }
        return json(['status' => 'failed', 'code'  =>  '99999']);
    }

}